//
//  BACityView.swift
//  BadAir
//
//  Created by D on 2018/3/9.
//  Copyright © 2018年 D. All rights reserved.
//

import UIKit

class BACityView: UIView {
    
    var cityModel: BAAQICityModel? {
        didSet{
            updateData()
        }
    }
    var weatherModel: BANowModel? {
        didSet{
            updateWeather()
        }
    }
    
    private let aqiLabel = UILabel()
    private let aqiValueLabel = UILabel()
    private let cityLabel = UILabel()
    private let tmpLabel = UILabel()
    private var iconWeather = UIImageView()
    
    private let pm25View = BAElementView()
    private let pm10View = BAElementView()
    private let pm10SubView = BAElementView()
    private let O3View = BAElementView()
    private let NO2View = BAElementView()
    private let COView = BAElementView()
    private let SO2View = BAElementView()
    
    private var eleWidth3: CGFloat = 0
    private var eleWidth4: CGFloat = 0
    private var eleSpace: CGFloat = 0
    //    private let promptLabel = UILabel()
    //    private let dataLabel = UILabel()
    
    
    
    
    private func setupUI() {
        let shadowImg = #imageLiteral(resourceName: "img_shadow").resizableImage(withCapInsets: UIEdgeInsets(top: 50,left: 50,bottom: 50,right: 50), resizingMode: UIImageResizingMode.stretch)
        let shadowView = UIImageView(image: shadowImg)
        addSubview(shadowView)
        
        let back1 = UIView()
        let back2 = UIView()
        back1.backgroundColor = colorWhite
        back2.backgroundColor = colorWhite
        back2.layer.cornerRadius = 15
        back2.layer.masksToBounds = true
        addSubview(back1)
        addSubview(back2)
        
        
        let iconCity = UIImageView(image: #imageLiteral(resourceName: "icon_home_city"))
        addSubview(iconCity)
        iconWeather = UIImageView(image: #imageLiteral(resourceName: "icon_weather_ri"))
        addSubview(iconWeather)
        
        cityLabel.setupLabel(ofSize: 15, weight: .regular, txtColor: UIColor("#12213D"))
        addSubview(cityLabel)
        tmpLabel.setupLabel(ofSize: 31, weight: .regular, txtColor: UIColor("#12213D"))
        addSubview(tmpLabel)
        aqiLabel.text = "AQI"
        aqiLabel.setupLabel(ofSize: 16, weight: .regular, txtColor: UIColor("#7D8CA0"))
        aqiValueLabel.setupLabel(ofSize: 32, weight: .regular, txtColor: UIColor("#12213D"))
        addSubview(aqiLabel)
        addSubview(aqiValueLabel)
        
        // 元素信息
        pm25View.titleType = .PM25
        pm10View.titleType = .PM10
        NO2View.titleType = .NO2
        SO2View.titleType = .SO2
        COView.titleType = .CO
        O3View.titleType = .O3
        addSubview(pm25View)
        addSubview(pm10View)
        addSubview(NO2View)
        addSubview(SO2View)
        addSubview(COView)
        addSubview(O3View)
        COView.alpha = 0
        O3View.alpha = 0
        
        
        
        // view约束
//        let eleWidth: CGFloat = 51
//        let eleHeight: CGFloat = 12
//        eleWidth4 = eleWidth * 4
//        let eleSpace4 = (screenWidth - eleWidth4) / 5
        // 位置
        eleSpace = 30
        pm25View.snp.makeConstraints { (make) in
            make.top.equalTo(iconWeather.snp.bottom).offset(32)
            make.left.equalTo(eleSpace)
//            make.width.equalTo(eleWidth)
//            make.height.equalTo(eleHeight)
        }
        NO2View.snp.makeConstraints { (make) in
            make.top.equalTo(pm25View)
            make.left.equalTo(pm25View.snp.right).offset(eleSpace)
//            make.width.height.equalTo(pm25View)
        }
        SO2View.snp.makeConstraints { (make) in
            make.top.equalTo(pm25View)
            make.left.equalTo(NO2View.snp.right).offset(eleSpace)
//            make.width.height.equalTo(pm25View)
        }
        pm10View.snp.makeConstraints { (make) in
            make.top.equalTo(pm25View)
            make.left.equalTo(SO2View.snp.right).offset(eleSpace)
//            make.width.height.equalTo(pm25View)
        }
        
        
        shadowView.snp.makeConstraints { (make) in
            make.height.equalTo(100)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(30)
        }
        
        back1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
        back2.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        iconCity.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(30)
            make.left.equalToSuperview().offset(47)
        }
        cityLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(iconCity)
            make.left.equalTo(iconCity.snp.right).offset(5)
        }
        iconWeather.snp.makeConstraints { (make) in
            make.top.equalTo(iconCity.snp.bottom).offset(21)
            make.left.equalTo(iconCity)
            make.height.width.equalTo(33)
            // 视图最高
            make.bottom.equalToSuperview().offset(-80)
        }
        tmpLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(iconWeather)
            make.left.equalTo(iconWeather.snp.right).offset(8)
        }
        
        
        aqiValueLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconWeather)
            make.left.greaterThanOrEqualTo(tmpLabel.snp.right).offset(60)
            make.right.lessThanOrEqualTo(-60)
        }
        aqiLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(aqiValueLabel.snp.top).offset(2)
            make.centerX.equalTo(aqiValueLabel)
        }
        
    }
    
    private func updateWeather() {
        tmpLabel.text = (weatherModel?.tmp ?? "-") + "°"
        iconWeather = UIImageView(image: BAWeatherViewModel.weatherCodeToIcon(code: weatherModel?.cond_code ?? ""))
    }
    
    private func updateData() {
        cityLabel.text = cityModel?.area
        aqiValueLabel.text = cityModel?.aqi
        
        pm25View.value = cityModel?.pm2_5
        pm10View.value = cityModel?.pm10
        pm10SubView.value = cityModel?.pm10
        NO2View.value = cityModel?.no2
        SO2View.value = cityModel?.so2
        COView.value = cityModel?.co
        O3View.value = cityModel?.o3
        
        
        
        // 位置
        eleWidth3 = pm25View.frame.width + NO2View.frame.width + SO2View.frame.width
        eleWidth4 = pm25View.frame.width + pm10View.frame.width + NO2View.frame.width + SO2View.frame.width
        let eleSpace3 = (screenWidth - eleWidth3) / 4
        let eleSpace4 = (screenWidth - eleWidth4) / 5
        eleSpace = eleSpace4
        
        pm25View.snp.updateConstraints { (make) in
            make.left.equalTo(eleSpace)
        }
        NO2View.snp.updateConstraints { (make) in
            make.left.equalTo(pm25View.snp.right).offset(eleSpace)
        }
        SO2View.snp.updateConstraints { (make) in
            make.left.equalTo(NO2View.snp.right).offset(eleSpace)
        }
        pm10View.snp.updateConstraints { (make) in
            make.left.equalTo(SO2View.snp.right).offset(eleSpace)
        }
        
        setNeedsLayout()
    }
    
    func updateElePoint() {
        
        print("pm25ViewWidth" , pm25View.frame.width )
        setNeedsLayout()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
